
package com.cib.ajax.rpc.util;


import org.jabsorb.JSONSerializer;
import org.jabsorb.serializer.SerializerState;

import com.cib.ajax.rpc.serializer.BigIntegerSerializer;
import com.cib.ajax.rpc.serializer.EnumSerializer;

public class JSON2JavaBean {
    private JSONSerializer serializer;

    public JSON2JavaBean() {
        super();
        serializer = new JSONSerializer();
        try {
            serializer.registerDefaultSerializers();
            EnumSerializer enumSerializer = new EnumSerializer();
            serializer.registerSerializer(enumSerializer);
            serializer.registerSerializer(new BigIntegerSerializer());
        } catch (Exception e) {
            //throw new TransformationException(e);
        }
        serializer.setMarshallClassHints(true);
        serializer.setMarshallNullAttributes(true);
    }

    public Object transform(Object source, Class clazz) {
        if (source == null) {
            return null;
        }

        try {
            SerializerState state = new SerializerState();
            return serializer.unmarshall(state, clazz, source);
        } catch (Exception e) {
            //throw new TransformationException(e);
        	return null;
        }

    }

    public int getWeight() {
        return 5000;
    }
}
